import axios from 'axios'
import {
  config
} from './config'
/* import {
  Toast
} from 'mint-ui'
import 'mint-ui/lib/style.css'

const tips = {
  1: '抱歉，出现了一个错误',
  1000: '输入参数错误',
  1001: '输入的json格式不正确',
  1002: '找不到资源',
  1003: '未知错误',
  1004: '禁止访问',
  1005: '不正确的开发者key',
  1006: '服务器内部错误',
  2000: '你已经点过赞了',
  2001: '你还没点过赞',
  3000: '该期内容不存在'
} */

class Complex {
  $axios ({url, data = {}, method = 'GET'}) {
    return new Promise((resolve, reject) => {
      this.request(url, resolve, reject, data, method)
    })
  }

  request (url, resolve, reject, data = {}, method = 'GET') {
    let param = method.toLocaleLowerCase() === 'get' ? 'params' : 'data'
    axios({
      url: config.classic_base_url + url,
      method: method,
      [param]: data,
      headers: {
        'Content-Type': 'application/json',
        'appkey': config.appkey
      }
    }).then(res => {
      const code = res.status.toString()
      if (code.startsWith('2')) {
        resolve(res.data)
      } else {
        reject()
      }
    })
  }
}

/* class Complex {
  $axios ({url, data = {}, method = 'GET'}) {
    return new Promise((resolve, reject) => {
      this.request(url, resolve, reject, data, method)
    })
  }

  request (url, resolve, reject, data = {}, method = 'GET') {
    axios({
      url: config.classic_base_url + url,
      method: method,
      data: data,
      headers: {
        'Content-Type': 'application/json',
        'appkey1': config.appkey
      }
    }).then(res => {
      const code = res.status.toString()
      if (code.startsWith('2')) {
        resolve(res.data)
      } else {
        reject()
        console.log(res)
        const errorCode = res.data.error_code
        this._showError(errorCode)
      }
    })
  }
  _showError (errorCode) {
    if (!errorCode) {
      errorCode = 1
    }
    const tip = tips[errorCode]
    Toast({
      message: tip,
      duration: 2000
    })
  }
} */
/* export function getMovieHomeInfo() {
  return axios({
    url: `${config.classic_base_url}/classic/latest`,
    method: 'GET',
    headers: {
      'Content-Type': 'application/json',
      'appkey': config.appkey
    }
  })
} */

export {
  Complex
}
